package com.deloitte.sapnetweaver.common
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.deloitte.sapnetweaver.events.FileDownLoadEvent;
	import com.deloitte.sapnetweaver.model.CommonModel;
	import com.deloitte.sapnetweaver.model.ModelLocator;
	
	import mx.controls.Alert;
	import mx.utils.StringUtil;

	public class CommonUtil
	{
		[Bindable]private static var model:ModelLocator = ModelLocator.getInstance();
		public static function isEmpty(input:String):Boolean{
			var flag:Boolean = false;
			if(input == null || StringUtil.isWhitespace(input) || StringUtil.trim(input).length == 0){
				flag = true;
			}
			return flag;
		}
		
		public static function downloadFile(relativePath:String):void{
			if(!isEmpty(relativePath)){
				var completeUrl:String = model.commonModel.fileDownloadContextPath+"/"+relativePath;
				
				var evt:FileDownLoadEvent=new FileDownLoadEvent(completeUrl);
				CairngormEventDispatcher.getInstance().dispatchEvent(evt); 
			}
		}
		
		public static function getDocIcon(filePath:String):String{
			var extension:String = getFileExtn(filePath);
			var iconName:String = model.commonModel.documentIconMap[extension]; 
			return iconName;
		}
		
		public static function getFileExtn(filePath:String):String{
			if(!isEmpty(filePath)){
				var extensionIndex:int = filePath.lastIndexOf( '.' );
				var extension:String = filePath.substr( extensionIndex + 1, filePath.length );
				return extension;
			}
			return "";
		}
		
		public static function getFileName(filePath:String):String{
			if(!isEmpty(filePath)){
				var nameIndex:int = filePath.lastIndexOf( '/' );
				var name:String = filePath.substr( nameIndex + 1, filePath.length );
				return name;
			}
			return "";
		}
		
		public static function getDocIconPath(filename:String):String{
			var iconName:String = getDocIcon(filename);
			if(!isEmpty(iconName)){
				return CommonModel.iconPath+iconName;
			}
			return null;
		}
	}
}